﻿
using Model.Core;
using Models;
using SCADA_DAQ.ZEMT.Server;
using SIE.SCADA.Common.Log;
using SIE.SCADA.SqlSugar;
using SIE.SCADA.ZemtDemo.Common;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SIE.SCADA.ZemtDemo.ViewModel
{
    public class EquipmentViewModel : QueryBaseViewModel<scada_equipment>
    {
        PgSqlAccess pgsql = new PgSqlAccess();
        EquipManager equipManager;

        public EquipmentViewModel()
        {
            equipManager = EquipManager.CreateInstance();
        }

        public override void Query()
        {
            using (var ctx = pgsql.GetClient())
            {
                Records = new List<scada_equipment>();
                if (pgsql.DBClient is SmdcOrmClient ormClient)
                {
                    Records = ormClient.SqlSugarClient.Queryable<scada_equipment>().ToList();
                }
            }
        }


        public  void Query(string equipmentCode,string workbin)
        {
            using (var ctx = pgsql.GetClient())
            {
                Records = new List<scada_equipment>();
                if (pgsql.DBClient is SmdcOrmClient ormClient)
                {
                    
                    string sql= " 1=1 " ;
                    if (equipmentCode!=null && equipmentCode !="") {
                        sql += " and equipmentid = '"+ equipmentCode+"'";
                    }
                    if (workbin != null && workbin != "")
                    {
                        sql += " and workbin = '" + workbin + "'";
                    }

                    Records = ormClient.SqlSugarClient.Queryable<scada_equipment>().Where(sql).ToList();


                }
            }
        }


        public override void Delete()
        {
            if (CurItem != null)
            {
                using (var ctx = pgsql.GetClient())
                {
                    if (pgsql.DBClient is SmdcOrmClient ormClient)
                    {
                        ormClient.SqlSugarClient.Deleteable<scada_equipment>().Where(m => m.id == CurItem.id).ExecuteCommand();
                    }
                }
                base.Delete();
            }
        }

        public override void Update()
        {
            if (CurItem !=null) {
                using (var ctx = pgsql.GetClient())
                {
                    if (pgsql.DBClient is SmdcOrmClient ormClient)
                    {
                        ormClient.SqlSugarClient.Updateable(CurItem).Where(m => m.id == CurItem.id).ExecuteCommand();
                    }
                }
                base.Update();
            }
            //if(string.IsNullOrEmpty(CurItem.equipmentid))
            //{
            //    equipManager.Request(CurItem.equipmentid);
            //}
        }

        

        public override void Add()
        {
            using (var ctx = pgsql.GetClient())
            {
                try
                {
                    if (pgsql.DBClient is SmdcOrmClient ormClient)
                    {
                        var equipment = new scada_equipment();
                        ormClient.SqlSugarClient.Insertable<scada_equipment>(equipment).IgnoreColumns(it => it == "id").ExecuteCommand();
                        Query();
                    }
                }
                catch (Exception ex)
                {
                    Logger.Error(ex.Message);
                    Logger.Error(ex.StackTrace);

                }
                base.Add();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        private void UpdateFromMq()
        {

        }

    }
}
